/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.core.enums;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(namespace = "http://gradecalculator.net/")
public final class ExaminationType extends AbstractEnumType {
	
	private static List<ExaminationType> types = new ArrayList<ExaminationType>(); 
	private static List<ExaminationType> temporaryResults = new ArrayList<ExaminationType>();

	public ExaminationType()
	{
	}
	
	private ExaminationType(final String name, final String abbreviation)
	{
		super(name, abbreviation);
	}

	public ExaminationType(final String value) {
		super("", value);
	}

	public static void reset()
	{
		types.clear();
	}
	
	public static void addExaminationType(final String name, final String abbreviation)
	{
		addExaminationType(new ExaminationType(name, abbreviation));
	}
	
	public int ordinal() {
		return types.indexOf(this);
	}
	
	public static ExaminationType[] values() {
		final ExaminationType[] result = new ExaminationType[types.size()];
		for (int i = 0; i < result.length; i++) {
			result[i] = types.get(i);
		}
		return result;
	}

	public static ExaminationType valueOf(final String type) {
		for (ExaminationType rt : types) {
			if (type.equals(rt.getAbbreviation())) {
				return rt;
			}
			if (type.equals(rt.getAbbreviation() + " - " + rt.getName())) {
				return rt;
			}
		}

		return null;
	}
	
	public int hashCode()
	{
		return getAbbreviation().hashCode();
	}
	
	public boolean equals(final Object o)
	{
		if (o == this) {
			return true;
		}
		
		if (!(o instanceof ExaminationType)) {
			return false;
		}
		
		final ExaminationType type = (ExaminationType)o;
		return (type.getAbbreviation().equals(this.getAbbreviation()));
	}

	public static void addExaminationType(final ExaminationType tmpType) {
		ExaminationType replaceItem = null;
		for (ExaminationType type : temporaryResults) {
			if (type.getAbbreviation().equals(tmpType.getAbbreviation())) {
				replaceItem = type;
				break;
			}
		}

		if (!tmpType.getName().equals("")) {
			if (replaceItem != null) {
				temporaryResults.remove(replaceItem);
				replaceItem.setName(tmpType.getName());
			} else {
				types.add(tmpType);
			}
		} else {
			if (replaceItem == null) {
				temporaryResults.add(tmpType);
				types.add(tmpType);
			}
		}
	}
}
